package com.zx.system.block.mapstruct;

import com.zx.core.context.TenantContextHolder;
import com.zx.core.enums.StatusEnum;
import com.zx.system.block.enums.*;
import com.zx.system.block.model.SysMenu;
import com.zx.system.block.param.MenuParam;
import com.zx.system.block.vo.SysMenuVO;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;

/**
 * <p>
 * description: 菜单映射参数 <br>
 * create: 2024-12-21 21:06
 * </p>
 *
 * @author zhou  xun
 */
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface MenuMapStructMapper {
    MenuMapStructMapper INSTANCE = Mappers.getMapper(MenuMapStructMapper.class);

    /**
     * 将MenuParam转换为SysMenu实体
     *
     * @param menuParam 菜单参数
     * @return SysMenu实体
     */
    SysMenu menuParamToSysMenu(MenuParam menuParam);

    /**
     * 将SysMenu实体转换为SysMenuVO视图对象
     *
     * @param sysMenu 菜单实体
     * @return SysMenuVO视图对象
     */
    SysMenuVO sysMenuToSysMenuVO(SysMenu sysMenu);

    /**
     * 在映射完成后，设置描述性字段
     *
     * @param sysMenu   菜单实体
     * @param sysMenuVO 菜单VO
     */
    @AfterMapping
    default void setDescriptions(SysMenu sysMenu, @MappingTarget SysMenuVO sysMenuVO) {
        // 设置菜单类型描述
        String menuType = sysMenu.getMenuType();
        MenuTypeEnum menuTypeEnum = MenuTypeEnum.getEnumByCode(menuType);
        if (menuTypeEnum != null) {
            sysMenuVO.setMenuTypeDesc(menuTypeEnum.getDesc());
        }

        // 设置状态描述
        Integer status = sysMenu.getStatus();
        StatusEnum statusEnum = StatusEnum.getEnumByCode(status);
        if (statusEnum != null) {
            sysMenuVO.setStatusDesc(statusEnum.getDesc());
        }

        // 设置隐藏描述
        Integer hidden = sysMenu.getHidden();
        HiddenEnum hiddenEnum = HiddenEnum.getEnumByCode(hidden);
        if (hiddenEnum != null) {
            sysMenuVO.setHiddenDesc(hiddenEnum.getDesc());
        }

        // 设置总是显示描述
        Integer alwaysShow = sysMenu.getAlwaysShow();
        AlwaysShowEnum alwaysShowEnum = AlwaysShowEnum.getEnumByCode(alwaysShow);
        if (alwaysShowEnum != null) {
            sysMenuVO.setAlwaysShowDesc(alwaysShowEnum.getDesc());
        }

        // 设置组件缓存描述
        Integer componentCache = sysMenu.getComponentCache();
        ComponentCacheEnum componentCacheEnum = ComponentCacheEnum.getEnumByCode(componentCache);
        if (componentCacheEnum != null) {
            sysMenuVO.setComponentCacheDesc(componentCacheEnum.getDesc());
        }

        // 设置固定描述
        Integer affix = sysMenu.getAffix();
        AffixEnum affixEnum = AffixEnum.getEnumByCode(affix);
        if (affixEnum != null) {
            sysMenuVO.setAffixDesc(affixEnum.getDesc());
        }

        // 设置面包屑描述
        Integer breadcrumb = sysMenu.getBreadcrumb();
        BreadcrumbEnum breadcrumbEnum = BreadcrumbEnum.getEnumByCode(breadcrumb);
        if (breadcrumbEnum != null) {
            sysMenuVO.setBreadcrumbDesc(breadcrumbEnum.getDesc());
        }
    }

    /**
     * 在映射完成后，设置租户名
     *
     * @param sysMenu   菜单实体
     * @param sysMenuVO 菜单VO
     */
    @AfterMapping
    default void setTenantName(SysMenu sysMenu, @MappingTarget SysMenuVO sysMenuVO) {
        sysMenuVO.setTenantName(TenantContextHolder.getTenantName());
    }
}